package algorithm.exam.queue;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class topKFrequent {

    public int[] topKFrequent(int[] nums, int k) {
        PriorityQueue<Map.Entry<Integer,Integer>> priorityQueue =new PriorityQueue<>((o1,o2)->o1.getValue()-o2.getValue());
        HashMap<Integer,Integer> map =new HashMap<>();
        for(int i=0;i<nums.length;i++){
            map.put(nums[i],map.getOrDefault(nums[i],0)+1);
        }
        priorityQueue.addAll(map.entrySet());
        while (priorityQueue.size()>k){
            priorityQueue.poll();
        }

        int[] res =new int[k];
        for(int m=0;m<k;m++){
            res[m]=priorityQueue.poll().getKey();
        }

        return res;



    }
}
